package com.lineage.server.timecontroller.pc;

import com.lineage.data.cmd.CreateNewItem;
import com.lineage.server.datatables.FishingTable;
import com.lineage.server.datatables.ItemTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_CharVisualUpdate;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Fishing;
import com.lineage.server.templates.L1Item;
import com.lineage.server.thread.PcOtherThreadPool;
import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class PcFishingTimer extends TimerTask {
  private static final Log _log = LogFactory.getLog(PcFishingTimer.class);
  
  private static final List<L1PcInstance> _fishingList = new ArrayList<>();
  
  private int _time = 0;
  
  private ScheduledFuture<?> _timer;
  
  public void start() {
    int timeMillis = 80000;
    this._timer = PcOtherThreadPool.get().scheduleAtFixedRate(this, 80000L, 80000L);
  }
  
  public void run() {
    try {
      fishing();
    } catch (Exception e) {
      _log.error("钓鱼时间轴异常重启", e);
      PcOtherThreadPool.get().cancel(this._timer, false);
      PcFishingTimer timerTask = new PcFishingTimer();
      timerTask.start();
    } 
  }
  
  public static void addMember(L1PcInstance pc) {
    if (pc == null || _fishingList.contains(pc))
      return; 
    _fishingList.add(pc);
  }
  
  public static void removeMember(L1PcInstance pc) {
    if (pc == null || !_fishingList.contains(pc))
      return; 
    _fishingList.remove(pc);
  }
  
  private void fishing() {
    if (!_fishingList.isEmpty()) {
      this._time++;
      int i = 0;
      while (i < _fishingList.size()) {
        L1PcInstance pc = _fishingList.get(i);
        if (pc.isFishing())
          if (pc.getMapId() != 5300) {
            finishFishing(pc, true);
          } else if (!pc.getInventory().checkItem(83002, 1L)) {
            finishFishing(pc, true);
          } else if (pc.getOnlineStatus() != 1) {
            finishFishing(pc, true);
          } else if (pc.getNetConnection() == null) {
            finishFishing(pc, true);
          } else if (pc.getFishingPoleId() == 0) {
            finishFishing(pc, true);
          } else {
            int fishingpoleid = pc.getFishingPoleId();
            L1ItemInstance fishingpole = pc.getInventory().findItemId(fishingpoleid);
            if (fishingpole != null && fishingpoleid != 83001) {
              L1Fishing fish = FishingTable.get().get_fish(fishingpoleid);
              successFishing(pc, fish);
              fishingpole.setChargeCount(fishingpole.getChargeCount() - 1);
              pc.getInventory().updateItem(fishingpole, 128);
              if (fishingpole.getChargeCount() <= 0) {
                pc.getInventory().consumeItem(fishingpoleid, 1L);
                L1ItemInstance newitem = ItemTable.get().createItem(83001);
                newitem.setIdentified(true);
                pc.getInventory().storeItem(newitem);
                pc.setFishingPoleId(83001);
              } 
            } else if (this._time == 3 && fishingpole != null && fishingpoleid == 83001) {
              L1Fishing fish = FishingTable.get().get_fish(fishingpoleid);
              successFishing(pc, fish);
            } 
          }  
        i++;
      } 
      if (this._time >= 3)
        this._time = 0; 
    } 
  }
  
  public static void finishFishing(L1PcInstance pc, boolean msg) {
    pc.setFishing(false, -1, -1);
    if (msg)
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(1163)); 
    pc.sendPacketsAll((ServerBasePacket)new S_CharVisualUpdate(pc));
    removeMember(pc);
  }
  
  private void successFishing(L1PcInstance pc, L1Fishing fish) {
    if (fish == null) {
      if (!pc.getInventory().consumeItem(83002, 1L))
        finishFishing(pc, true); 
    } else {
      L1Item item = ItemTable.get().getTemplate(fish.get_itemid());
      if (item != null)
        if (pc.getInventory().consumeItem(83002, 1L)) {
          CreateNewItem.createNewItem(pc, fish.get_itemid(), 1L);
        } else {
          finishFishing(pc, true);
        }  
    } 
  }
}
